var e = require("@vant/area-data"), t = i(require("@vant/weapp/toast/toast")), a = i(require("@vant/weapp/dialog/dialog"));

function i(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var d = require("../../utils/request"), s = require("../../utils/api"), o = require("../../utils/storage"), r = require("../../utils/date"), n = require("../../utils/encrypt"), c = require("../../utils/check");

Page({
    data: {
        themeColor: getApp().globalData.themeColor,
        showSelectBirthdayPopup: !1,
        showSelectAddressPopup: !1,
        maxDate: new Date().getTime(),
        minDate: new Date(1900, 1, 1).getTime(),
        currentDate: new Date(1990, 0, 1).getTime(),
        saveLoading: !1,
        deleteLoading: !1,
        areaList: e.areaList,
        personId: "",
        addressId: "",
        relationship: "",
        nameShow: "",
        nameValue: "",
        nameIcon: "eye-o",
        nameDisabled: !1,
        gender: "",
        marry: "",
        mobileShow: "",
        mobileValue: "",
        mobileIcon: "eye-o",
        mobileDisabled: !1,
        certificateType: "",
        certificateNoShow: "",
        certificateNoValue: "",
        certificateNoIcon: "eye-o",
        certificateNoDisabled: !1,
        birthday: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        addressStrShow: "",
        address: ""
    },
    onLoad: function(e) {
        var t = "";
        "" != e.provinceCode && null != e.provinceCode && (t += this.data.areaList.province_list[e.provinceCode] + " "), 
        "" != e.cityCode && null != e.cityCode && (t += this.data.areaList.city_list[e.cityCode] + " "), 
        "" != e.districtCode && null != e.districtCode && (t += this.data.areaList.county_list[e.districtCode]), 
        "{}" != JSON.stringify(e) && (null == e.birthday && (e.birthday = ""), "" != e.birthday && this.setData({
            currentDate: Number(e.birthday)
        }), this.setData({
            personId: e.personId,
            addressId: e.addressId,
            relationship: e.relationship,
            nameShow: e.name,
            nameValue: e.name,
            gender: e.gender,
            marry: e.marry,
            mobileShow: e.mobile,
            mobileValue: e.mobile,
            certificateType: e.certificateType,
            certificateNoShow: e.certificateNo,
            certificateNoValue: e.certificateNo,
            birthday: "" == e.birthday ? "" : r.formatDate(new Date(Number(e.birthday)), "yyyy-MM-dd"),
            provinceCode: e.provinceCode,
            cityCode: e.cityCode,
            districtCode: e.districtCode,
            addressStrShow: t,
            address: e.address
        }));
    },
    relationshipChange: function(e) {
        this.setData({
            relationship: e.detail
        });
    },
    genderChange: function(e) {
        this.setData({
            gender: e.detail
        });
    },
    marryChange: function(e) {
        this.setData({
            marry: e.detail
        });
    },
    certificateTypeChange: function(e) {
        this.setData({
            certificateType: e.detail
        });
    },
    nameIconClick: function() {
        this.setData({
            nameDisabled: !this.data.nameDisabled,
            nameIcon: "closed-eye" == this.data.nameIcon ? "eye-o" : "closed-eye",
            nameShow: n.encryptName(this.data.nameValue)
        });
    },
    certificateNoChange: function() {
        if ("0" == this.data.certificateType) {
            var e = c.getBirthDayFromIdCard(this.data.certificateNoValue), t = new Date(e).getTime();
            if (isNaN(t)) this.setData({
                birthday: "",
                currentDate: new Date(1990, 0, 1).getTime()
            }); else {
                var a = r.formatDate(new Date(Number(t)), "yyyy-MM-dd");
                this.setData({
                    birthday: a,
                    currentDate: t
                });
            }
        }
    },
    mobileIconClick: function() {
        this.setData({
            mobileDisabled: !this.data.mobileDisabled,
            mobileIcon: "closed-eye" == this.data.mobileIcon ? "eye-o" : "closed-eye",
            mobileShow: n.encryptMobile(this.data.mobileValue)
        });
    },
    certificateNoIconClick: function() {
        this.setData({
            certificateNoDisabled: !this.data.certificateNoDisabled,
            certificateNoIcon: "closed-eye" == this.data.certificateNoIcon ? "eye-o" : "closed-eye",
            certificateNoShow: n.encryptIdCard(this.data.certificateNoValue)
        });
    },
    onClickSelectBirhtday: function() {
        this.setData({
            showSelectBirthdayPopup: !0
        });
    },
    onCloseSelectBirthday: function() {
        this.setData({
            showSelectBirthdayPopup: !1
        });
    },
    confirmSelectBirthday: function(e) {
        this.setData({
            showSelectBirthdayPopup: !1,
            birthday: r.formatDate(e.detail, "yyyy-MM-dd")
        });
    },
    onClickSelectAddress: function() {
        this.setData({
            showSelectAddressPopup: !0
        });
    },
    onCloseSelectAddress: function() {
        this.setData({
            showSelectAddressPopup: !1
        });
    },
    confirmSelectAddress: function(e) {
        var a = "", i = e.detail.values, d = !0;
        i.forEach(function(e) {
            null != e && null != e.code && null != e.name && "" != e.code && "" != e.name ? a += e.name + " " : d = !1;
        }), d ? (this.setData({
            provinceCode: i[0].code,
            cityCode: i[1].code,
            districtCode: i[2].code
        }), this.setData({
            showSelectAddressPopup: !1,
            addressStrShow: a
        })) : (0, t.default)({
            message: "请选择完整的省市区",
            duration: 5e3
        });
    },
    save: function() {
        var e = this;
        "" != this.data.relationship ? "" != this.data.nameValue ? "0" == this.data.gender || "1" == this.data.gender ? "0" == this.data.marry || "1" == this.data.marry ? "" != this.data.mobileValue ? c.isMobile(this.data.mobileValue) ? "" != this.data.certificateType ? "" != this.data.certificateNoValue ? "0" != this.data.certificateType || c.isIdCard(this.data.certificateNoValue) ? "" != this.data.birthday ? (wx.showLoading({
            title: "正在保存...",
            mask: !0
        }), this.setData({
            saveLoading: !0
        }), "" == this.data.personId || null == this.data.personId ? d.request(d.HttpMethod.POST, s.apiRoute.wcaInsertMyFamily, {
            relationship: this.data.relationship,
            name: this.data.nameValue,
            gender: this.data.gender,
            marry: this.data.marry,
            mobile: this.data.mobileValue,
            certificateType: this.data.certificateType,
            certificateNo: this.data.certificateNoValue,
            birthday: this.data.birthday,
            provinceCode: this.data.provinceCode,
            cityCode: this.data.cityCode,
            districtCode: this.data.districtCode,
            address: this.data.address
        }).then(function(a) {
            "20000" == a.code ? ((0, t.default)({
                message: "保存成功",
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            }), wx.navigateBack({
                delta: 1
            })) : ((0, t.default)({
                message: "".concat(a.message),
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            }));
        }) : d.request(d.HttpMethod.POST, s.apiRoute.wcaUpdateMyFamily, {
            personId: this.data.personId,
            addressId: this.data.addressId,
            relationship: this.data.relationship,
            name: this.data.nameValue,
            gender: this.data.gender,
            marry: this.data.marry,
            mobile: this.data.mobileValue,
            certificateType: this.data.certificateType,
            certificateNo: this.data.certificateNoValue,
            birthday: this.data.birthday,
            provinceCode: this.data.provinceCode,
            cityCode: this.data.cityCode,
            districtCode: this.data.districtCode,
            address: this.data.address
        }).then(function(a) {
            if ("20000" == a.code) {
                if ((0, t.default)({
                    message: "保存成功",
                    duration: 5e3
                }), wx.hideLoading(), e.setData({
                    saveLoading: !1
                }), wx.navigateBack({
                    delta: 1
                }), "0" == e.data.relationship) {
                    var i = o.getStorageSync(o.storageKey.userBase);
                    i.sex = e.data.gender, i.name = e.data.nameValue, i.certificateNo = e.data.certificateNoValue, 
                    i.certificateType = e.data.certificateType, o.setStorageSync(o.storageKey.userBase, i);
                }
            } else (0, t.default)({
                message: "".concat(a.message),
                duration: 5e3
            }), wx.hideLoading(), e.setData({
                saveLoading: !1
            });
        })) : (0, t.default)({
            message: "请输入出生日期",
            duration: 5e3
        }) : (0, t.default)({
            message: "身份证格式不正确",
            duration: 5e3
        }) : (0, t.default)({
            message: "请输入证件号",
            duration: 5e3
        }) : (0, t.default)({
            message: "请选择证件类型",
            duration: 5e3
        }) : (0, t.default)({
            message: "手机号格式不正确",
            duration: 5e3
        }) : (0, t.default)({
            message: "请输入手机号",
            duration: 5e3
        }) : (0, t.default)({
            message: "请选择婚否",
            duration: 5e3
        }) : (0, t.default)({
            message: "请选择性别",
            duration: 5e3
        }) : (0, t.default)({
            message: "请输入姓名",
            duration: 5e3
        }) : (0, t.default)({
            message: "请选择与本人关系",
            duration: 5e3
        });
    },
    delete: function() {
        var e = this;
        "" != this.data.personId ? "0" != this.data.relationship ? a.default.confirm({
            title: "警告",
            message: "您确定要删除吗？"
        }).then(function() {
            wx.showLoading({
                title: "正在删除...",
                mask: !0
            }), e.setData({
                deleteLoading: !0
            }), d.request(d.HttpMethod.POST, s.apiRoute.wcaDeleteMyFamily, {
                personId: e.data.personId,
                addressId: e.data.addressId
            }).then(function(a) {
                "20000" == a.code ? ((0, t.default)({
                    message: "删除成功",
                    duration: 5e3
                }), wx.hideLoading(), e.setData({
                    deleteLoading: !1
                }), wx.navigateBack({
                    delta: 1
                })) : ((0, t.default)({
                    message: "删除失败，".concat(a.message),
                    duration: 5e3
                }), wx.hideLoading(), e.setData({
                    deleteLoading: !1
                }));
            }).catch(function() {
                console.log("未删除");
            });
        }) : (0, t.default)({
            message: "本人信息不允许删除",
            duration: 5e3
        }) : (0, t.default)({
            message: "该数据未保存无需删除",
            duration: 5e3
        });
    }
});